package com.example.big_job.Adapter;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.big_job.DB.DBHelper;
import com.example.big_job.FoodActivity;
import com.example.big_job.MeauActivity;
import com.example.big_job.OrderActivity;
import com.example.big_job.R;
import com.example.big_job.StoreActivity;

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private final int type_zero = 0;
    private final int type_one = 1;
    private Context context;
    private List<Integer> mDatas;
    public OrderAdapter(Context context, List<Integer> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == type_zero) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_prize, parent, false);
            return new prizeViewHolder(view);
        }
        else{
            view= LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
            return new orderViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        DBHelper sqlite = new DBHelper(context, 1);
        SQLiteDatabase database = sqlite.getReadableDatabase();
        if(holder.getItemViewType()==type_zero) {
            prizeViewHolder prizeViewHolder = (prizeViewHolder) holder;
            Cursor cur=database.rawQuery("SELECT SUM(prize) FROM food",null);

            if (cur.getCount()>0){
                if (cur.moveToFirst()){
                    prizeViewHolder.sumprize.setText(cur.getString(0));
                }
            }else{
                prizeViewHolder.sumprize.setText("0");
            }


            prizeViewHolder.fanhui.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=null;

                    intent =new Intent(view.getContext(), MeauActivity.class);

                    view.getContext().startActivity(intent);
                }
            });
            prizeViewHolder.pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DBHelper sqlite = new DBHelper(context,1);
                    SQLiteDatabase database =sqlite.getReadableDatabase();
                    Cursor Raw = database.rawQuery("select * from food ", null);
                    int orderNum=Raw.getCount();
                    if (orderNum>0){
                        Toast.makeText(context.getApplicationContext(), "正在跳转到支付页面，请稍后....", Toast.LENGTH_SHORT).show();

                        database.execSQL("DELETE FROM food");
                        database.execSQL("update sqlite_sequence SET seq = 0 where name ='food'");


                        Intent intent=null;

                        intent =new Intent(view.getContext(), MeauActivity.class);

                        view.getContext().startActivity(intent);
                        Toast.makeText(context.getApplicationContext(), "支付成功！", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(context.getApplicationContext(), "您没有未支付的订单，无需支付！", Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }
        else{
            orderViewHolder orderviewHolder=(OrderAdapter.orderViewHolder) holder;

//            String sp=String.valueOf(position);


            if (position==1){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"1"});
//                int n=Rawcursor2.getCount();
//                String ns=String.valueOf(n);
//                Log.i("num",ns);
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
//                    orderviewHolder.storename.setText(Rawcursor2.getString(3));
                }
            }
            else if (position==2){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"2"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==3){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"3"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==4){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"4"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==5){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"5"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==6){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"6"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==7){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"7"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==8){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"8"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==9){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"9"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }else if (position==10){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"10"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }else if (position==11){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"11"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==12){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"12"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==13){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"13"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }
            else if (position==14){
                Cursor Rawcursor2 = database.rawQuery("select * from food where id=?", new String[]{"14"});
                while (Rawcursor2.moveToNext()){
                    orderviewHolder.foodname.setText(Rawcursor2.getString(1));
                    orderviewHolder.foodprize.setText(Rawcursor2.getString(2));
                }
            }




        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return type_zero;
        } else {
            return type_one;
        }
    }

    @Override
    public int getItemCount() {
        DBHelper sqlite = new DBHelper(context,1);
        SQLiteDatabase database =sqlite.getReadableDatabase();
        Cursor Rawcursor = database.rawQuery("select * from food ", new String[]{});
        int orderNum=Rawcursor.getCount();
        String ordernum0=String.valueOf(orderNum);
        Log.i("TAG",ordernum0);
        return orderNum+1;
    }
    public class orderViewHolder extends RecyclerView.ViewHolder{
        private TextView foodname;
        private TextView foodprize;
        private TextView storename;



        public orderViewHolder(@NonNull View itemView) {
            super(itemView);
            foodname=itemView.findViewById(R.id.orderfoodname);
            foodprize=itemView.findViewById(R.id.orderprize);
            storename=itemView.findViewById(R.id.storeaddress);

        }

    }
    public class prizeViewHolder extends RecyclerView.ViewHolder{
        private Button pay;
        private Button fanhui;
        private TextView sumprize;


        public prizeViewHolder(@NonNull View itemView) {
            super(itemView);
            pay=itemView.findViewById(R.id.pay);
            sumprize=itemView.findViewById(R.id.sumprize);
            fanhui=itemView.findViewById(R.id.prizereturn);
        }

    }
}
